package com.xl.basic.controller;

/**
 * @author 钟勇
 * Create in 2023/8/24 16:21
 */

import com.xl.basic.annotation.LoginIgnored;
import com.xl.basic.annotation.LoginValidated;
import com.xl.basic.entity.Result;
import com.xl.basic.entity.ResultEnums;
import com.xl.basic.entity.UserInfo;
import com.xl.basic.entity.param.LoginByPasswordParam;
import com.xl.basic.entity.param.UpdCurrUserPasswordParam;
import com.xl.basic.entity.param.UpdUserParam;
import com.xl.basic.entity.param.UpdUserPasswordParam;
import com.xl.basic.entity.result.LoginResult;
import com.xl.basic.service.LoginService;
import com.xl.basic.service.UserService;
import com.xl.basic.utils.LoginUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 钟勇
 * Create in 2023/8/24 9:57
 */
@Tag(name = "登录")
@RestController
@RequestMapping("/Login")
@RequiredArgsConstructor
@LoginIgnored
public class LoginController {
    private final LoginService loginService;
    private final UserService userService;

    @Operation(summary = "使用密码登录")
    @PostMapping(value = "/loginByPassword")
    Result<LoginResult> loginByPassword(@Validated LoginByPasswordParam param) {
        return loginService.loginByPassword(param);
    }


    @Operation(summary = "退出登录")
    @PostMapping(value = "/logout")
    public Result<Object> logout() {
        LoginUtils.clearUserInfo();
        return ResultEnums.Success.getResult();
    }

    @Operation(summary = "获取当前的登录信息")
    @PostMapping(value = "/getCurrUserInfo")
    public Result<UserInfo> getCurrUserInfo() {
        return LoginUtils.getUserInfo();
    }

    @LoginValidated
    @Operation(summary = "编辑当前的登录信息")
    @PostMapping(value = "/updCurrUserInfo")
    public Result<Object> updCurrUserInfo(@Validated UpdUserParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        UserInfo userInfo = userInfoRes.getResult();

        param.setUserId(userInfo.getUserId());
        Result<Object> updRes = userService.updUser(param, userInfo);
        if (updRes.isSuc()) {
            LoginUtils.getUserInfo(userInfo.getUserId());
        }

        return updRes;
    }


    @LoginValidated
    @Operation(summary = "修改当前用户的登录密码")
    @PostMapping(value = "/updCurrUserPassword")
    public Result<Object> updCurrUserPassword(@Validated UpdCurrUserPasswordParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        UserInfo userInfo = userInfoRes.getResult();

        Result<LoginResult> loginRes = loginService.loginByPassword(new LoginByPasswordParam(userInfo.getPhone(), param.getOriPassword()));

        if (loginRes.isErr()) {
            return loginRes.create();
        }

        Result<Object> updRes = userService.updUserPassword(new UpdUserPasswordParam(userInfo.getUserId(), param.getPassword()), userInfo);
        if (updRes.isSuc()) {
            //退出登录
            LoginUtils.setUserInfo(null);
        }

        return updRes;
    }

}
